package com.bxd.baodan.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 处理edittext的帮助类
 * 
 * @author oldfeel
 * 
 *         Create on: 2014年7月4日
 */
public class ETUtil {
	/**
	 * 获取edittext中内容
	 * 
	 * @param et
	 * @return
	 */
	public static String getString(EditText et) {
		return et.getText().toString().trim();
	}
	/**
	 * 获取TextView中内容
	 * 
	 * @param et
	 * @return
	 */
	public static String getString(TextView et) {
		return et.getText().toString().trim();
	}
	
	/**
	 * 获取textview中内容
	 * 
	 * @param et
	 * @return
	 */
	public static String getStringText(TextView tv) {
		return tv.getText().toString().trim();
	}

	/**
	 * 获取edittext中的int类型
	 * 
	 * @param et
	 * @return
	 */

	public static int getInt(EditText et) {
		String string = getString(et);
		if (string == null || string.length() == 0) {
			return 0;
		}
		string = string.replaceAll("[//d]", "");
		return Integer.valueOf(string);
	}

	public static String getHintString(EditText et) {
		return et.getHint().toString().trim();
	}

	/**
	 * 是否不为空
	 * 
	 * @param editTexts
	 * @return true 为非空,false为空
	 */
	public static boolean isNotNull(EditText... editTexts) {
		for (EditText editText : editTexts) {
			String content = editText.getText().toString().trim();
			if (content == null || content.length() == 0) {
				editText.setError("必填");
				editText.requestFocus();
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断edittext是否为空
	 * 
	 * @param editTexts
	 * @return
	 */
	public static boolean isEmpty(EditText... editTexts) {
		for (EditText editText : editTexts) {
			String content = editText.getText().toString().trim();
			if (content == null || content.length() == 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断edittext数字是否为O
	 * 
	 * @param editTexts
	 * @return
	 */
	public static boolean isZore(EditText... editTexts) {
		for (EditText editText : editTexts) {
			String content = editText.getText().toString().trim()
					.replace("元", "");
			if (content == null || Double.valueOf(content) == 0.00
					|| Double.valueOf(content) == 0.0) {
				if (content == null || Double.valueOf(content) == 0.00
						|| Double.valueOf(content) == 0.0
						|| Double.valueOf(content) == .00) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 将edittext回复为空
	 * 
	 * @param editTexts
	 */
	public static void setEmpty(EditText... editTexts) {
		for (EditText editText : editTexts) {
			editText.setText("");
		}
	}

	/**
	 * 判断edittext值是否为手机号码
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(EditText mobiles) {
//		Pattern p = Pattern
//				.compile("^((13[0-9])|(14[5,7])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$");

		Pattern p = Pattern
				.compile("^1[0-9]{10}$");
		Matcher m = p.matcher(getString(mobiles));
		return m.matches();
	}
	/**
	 * 判断edittext值是否为车牌号
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isCarNum(EditText mobiles){
		Pattern p = Pattern
				.compile("^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{5}$");
		Matcher m = p.matcher(getString(mobiles));
		return m.matches();
	}
	/**
	 * 判断textview值是否为手机号码
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobile(TextView mobiles) {
		Pattern p = Pattern
				.compile("^((13[0-9])|(14[5,7])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$");
		Matcher m = p.matcher(getStringText(mobiles));
		return m.matches();
	}

	/**
	 * 隐藏软键盘
	 * 
	 * @param editText
	 */
	public static void hideSoftKeyboard(Context context, EditText editText) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
	}

	/**
	 * 弹出软键盘
	 */
	public static void showSoftKeyboard(Context context, EditText editText) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(editText, 0);
	}

	/**
	 * 判断两个edittext内容是否一致
	 * 
	 * @param et1
	 * @param et2
	 * @return
	 */
	public static boolean isEquals(EditText et1, EditText et2) {
		if (isEmpty(et1) || isEmpty(et2)) {
			return false;
		}
		return getString(et1).equals(getString(et2));
	}

	/**
	 * 将edittext中光标移动到尾部
	 * 
	 *
	 */
	public static void setEnd(EditText et) {
		if (!isEmpty(et)) {
			et.setSelection(getString(et).length());
		}
	}

	/**
	 * 设置editetext是否可编辑
	 * 
	 * @param focusable
	 * @param editTexts
	 */
	public static void setETFocus(boolean focusable, EditText... editTexts) {
		for (EditText editText : editTexts) {
			editText.setFocusable(focusable);
		}
	}

	public static void setEnable(boolean isEnable, Context context,
			EditText... editTexts) {
		for (EditText editText : editTexts) {
			editText.setEnabled(isEnable);
		}

	}

	/**
	 * 是否是身份证号码
	 * 
	 * @param etIdentyNum
	 * @return
	 */
	public static boolean isIdentyNum(EditText etIdentyNum) {
		if (getString(etIdentyNum).length() == 15
				|| getString(etIdentyNum).length() == 18) {
			return true;
		}
		return false;
	}
	/**
	 * 普通车牌号校验
	 * @param etCarNumber
	 * @return
	 */
	public static boolean isCarNumber(EditText etCarNumber) {
		Pattern p = Pattern
				.compile("^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{4}[A-Z_0-9_\u4e00-\u9fa5]$");
		Matcher m = p.matcher(getStringText(etCarNumber));
		return m.matches();
	}

}
